﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GsnHelper;
using ZhongheCms.Models;

namespace ZhongheCms.Controllers
{
    public class ProPicController : Controller
    {
        public ActionResult Index(int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;

            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var pid = 7;
            if (!string.IsNullOrEmpty(p))
            {
                var funs = GswTblFunction.SingleOrDefault("where FuncName='产品信息' and ActionParam='" + p + "'");
                if (funs != null)
                {
                    pid = funs.Id;
                }
            }
            var qry = GswTblProInfo.GetProList(keywords,pid, GswTblUser.CurrentUser.CustomerId);
            return View(qry);
        }
        public ActionResult Create(int id = 0,int picid=0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;

            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var pro =GswTblProInfo.SingleOrDefault(id);
            pro.GswTblProPic = picid == 0 ? new GswTblProPic{PicNo = 1} : GswTblProPic.SingleOrDefault(picid);
            pro.GswTblProPiclist = GswTblProPic.Fetch("where ProId=" + id + " order by PicNo");
            if (picid != 0) return View(pro);
            var qry =
                GswTblProPic.FirstOrDefault("select max(PicNo) as PicNo from GswTblProPic where ProId=" + id);
            if (qry != null)
                pro.GswTblProPic.PicNo = qry.PicNo + 1;


            return View(pro);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection c, int id = 0,int picid=0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
        
            var pro =  GswTblProInfo.SingleOrDefault(id);
            var gswTblProPic = picid == 0 ? new GswTblProPic () :GswTblProPic.SingleOrDefault(picid);
            gswTblProPic.ProId = id;
            gswTblProPic.PicNo = InputValid.StrToInt(c["PicNo"], 0);
            gswTblProPic.PicTitle = c["PicTitle"];
            gswTblProPic.PicDesc = c["PicDesc"];
            gswTblProPic.PicFileName = c["PicFileName"];
           
            try
            {
                if (ModelState.IsValid)
                {

                    if (picid == 0)
                    {
                        gswTblProPic.Insert();

                        gswTblProPic.PicNo = InputValid.StrToInt(c["PicNo"], 0)+1;
                        gswTblProPic.PicTitle = "";
                        gswTblProPic.PicDesc = "";
                        gswTblProPic.PicFileName ="";
                        gswTblProPic.Id = 0;
                    }
                    else
                    {
                        gswTblProPic.Update();

                    }
                   
                    ViewBag.scode = "1";
                }

            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";
            pro.GswTblProPic =gswTblProPic;
            pro.GswTblProPiclist = GswTblProPic.Fetch("where ProId=" + id + " order by PicNo");
            return View(pro);

        }

        public ActionResult Delete(int id,int picid, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;

            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblProPic.Delete(picid);

            var pro = GswTblProInfo.SingleOrDefault(id);
            pro.GswTblProPic = new GswTblProPic { PicNo = 1 } ;
            pro.GswTblProPiclist = GswTblProPic.Fetch("where ProId=" + id + " order by PicNo");
            var qry =
             GswTblProPic.FirstOrDefault("select max(PicNo) as PicNo from GswTblProPic where ProId=" + id);
            if (qry != null)
                pro.GswTblProPic.PicNo = qry.PicNo + 1;

            return View("Create", pro);
        }

    }
}
